/**
  * 选择了ak或使用IP白名单校验：
  */

package com.sky.utils.ride;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.util.UriUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;
import java.util.Map;

public class RideCheck {

    public static String URL = "https://api.map.baidu.com/geocoding/v3?";

    public static String AK = "wQtpkTKt2rFMQoll23D9X8NcXJYLN62s";

    // public static String shopAddress = "北京市市辖区海淀区上地十街10号";

    public static String shopAddress = "北京市海淀区上地十街10号";

    // public String userAddress = "北京市海淀区上地十街18号";

    // public String userAddress = "上海市浦东新区陆家嘴东路158号";

    // public String userAddress = "厦门市思明区厦大白城海滨浴场附近";

    // public String userAddress = "北京市丰台区丰台南路128号";

    public static void rideCheck(String userAddress) throws Exception {

        RideCheck shopR = new RideCheck();
        Map params = new LinkedHashMap<String, String>();
        params.put("address", shopAddress);
        params.put("output", "json");
        params.put("ak", AK);
        String shopResult = shopR.requestGetAK(URL, params);
        String[] shopSplit = shopResult.split(",");
        String shopLng = shopSplit[0];
        String shopLat = shopSplit[1];
        System.out.println("商店地址 经度:" + shopLng + " "+ "维度:" + shopLat);

        RideCheck userR = new RideCheck();
        Map userParams = new LinkedHashMap<String, String>();
        userParams.put("address", userAddress);
        userParams.put("output", "json");
        userParams.put("ak", AK);
        String userResult = userR.requestGetAK(URL, userParams);
        String[] userSplit = userResult.split(",");
        String userLng = userSplit[0];
        String userLat = userSplit[1];
        System.out.println("用户地址 经度:" + userLng + " "+ "维度:" + userLat);

        Riding.riding(shopLat + "," + shopLng, userLat + "," + userLng);
    }

    /**
     * 默认ak
     * 选择了ak，使用IP白名单校验：
     * 根据您选择的AK已为您生成调用代码
     * 检测到您当前的ak设置了IP白名单校验
     * 您的IP白名单中的IP非公网IP，请设置为公网IP，否则将请求失败
     * 请在IP地址为0.0.0.0/0 外网IP的计算发起请求，否则将请求失败
     */
    public static String requestGetAK(String strUrl, Map<String, String> param) throws Exception {
        if (strUrl == null || strUrl.length() <= 0 || param == null || param.size() <= 0) {
            return null;
        }

        StringBuffer queryString = new StringBuffer();
        queryString.append(strUrl);
        for (Map.Entry<?, ?> pair : param.entrySet()) {
            queryString.append(pair.getKey() + "=");
            //    第一种方式使用的 jdk 自带的转码方式  第二种方式使用的 spring 的转码方法 两种均可
            //    queryString.append(URLEncoder.encode((String) pair.getValue(), "UTF-8").replace("+", "%20") + "&");
            queryString.append(UriUtils.encode((String) pair.getValue(), "UTF-8") + "&");
        }

        if (queryString.length() > 0) {
            queryString.deleteCharAt(queryString.length() - 1);
        }

        java.net.URL url = new URL(queryString.toString());
        // System.out.println(queryString.toString());
        URLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.connect();

        InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuffer buffer = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        isr.close();
        // System.out.println("AK: " + buffer.toString());
        JSONObject jsonObject = JSON.parseObject(buffer.toString());
        System.out.println(jsonObject);
        JSONObject result = jsonObject.getJSONObject("result");
        JSONObject location = result.getJSONObject("location");
        String lng = location.getString("lng");
        String lat = location.getString("lat");
        return lng + "," + lat;
    }
}